package com.work.yly.website.service;

import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.work.yly.website.vo.Hyxx;

import net.sf.json.JSONObject;

public class ServiceHelper {

	/**
	 * 会员登录
	 * 
	 * @param userName
	 * @param passWord
	 * @return
	 */
	public static JSONObject login(String userName, String passWord) {
		return post(ServiceDefine.SVR_LOGIN, String.format(ServiceDefine.SVR_LOGIN_PARAM, userName, passWord));
	}

	/**
	 * 会员注册
	 * 
	 * @param regInfo
	 * @return
	 */
	public static JSONObject register(Map<String, Object> regInfo) {
		return post(ServiceDefine.SVR_REGISTER, regInfo);
	}

	/**
	 * 获取理财产品信息
	 * 
	 * @return
	 */
	public static JSONObject lccplist() {
		return post(ServiceDefine.SVR_LCCPLIST, "{}");
	}

	/**
	 * 生成验证码
	 * 
	 * @param sjhm
	 * @param yzmyt
	 * @return
	 */
	public static JSONObject getYzm(String sjhm, String yzmyt) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("sjhm", sjhm);
		map.put("yzmyt", yzmyt);
		return post(ServiceDefine.SVR_GENYZM, map);
	}

	/**
	 * 忘记密码
	 * 
	 * @param sjhm
	 * @param dxyzm
	 * @param password
	 * @return
	 */
	public static JSONObject restpwd(String sjhm, String dxyzm, String password) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("sjhm", sjhm);
		map.put("dxyzm", dxyzm);
		map.put("password", password);
		return post(ServiceDefine.SVR_RESTPWD, map);
	}

	/**
	 * 修改密码
	 * 
	 * @param zh_id
	 * @param oldpassword
	 * @param newpassword
	 * @return
	 */
	public static JSONObject modpwd(int zh_id, String oldpassword, String newpassword) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("zhid", zh_id);
		map.put("oldpassword", oldpassword);
		map.put("newspassword", newpassword);
		return post(ServiceDefine.SVR_MODPWD, map);
	}

	/**
	 * 更新会员基本信息
	 * @param hy_id
	 * @param hyxm
	 * @param sfzh
	 * @param sex
	 * @param lxdh
	 * @param csrq
	 * @return
	 */
	public static JSONObject modHybaseInfo(Hyxx hyxx){
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("hy_id", hyxx.getId());
		map.put("hyxm", hyxx.getHyxm());
		map.put("sfzh", hyxx.getSfzh());
		map.put("sex", hyxx.getSex());
		map.put("lxdh", hyxx.getLxdh());
		map.put("csrq", DateUtils.formatDate(hyxx.getCsrq()));
		map.put("hy_lxr", Lists.newArrayList());
		return post(ServiceDefine.SVR_HYBASE, map);
	}

	/**
	 * 更新会员借款基本信息
	 * @param map
	 * @return
	 */
	public static JSONObject modHyJkInfo(Hyxx hyxx, String jklx){
		Map<String, Object> map = Maps.newHashMap();
		map.put("hy_id", hyxx.getId());
		map.put("address1", hyxx.getAddress1());
		map.put("address2", hyxx.getAddress2());
		map.put("qq", hyxx.getQq());
		map.put("nsr", hyxx.getNsr());
		map.put("fczk", hyxx.getFczk());
		map.put("zgxl", hyxx.getZgxl());
		map.put("hyzk", hyxx.getHyzk());
		map.put("dwmc", hyxx.getDwmc());
		map.put("dwxz", hyxx.getDwxz());
		map.put("dwdh", hyxx.getDwdh());
		map.put("dwdz1", hyxx.getDwdz1());
		map.put("dwdz2", hyxx.getDwdz2());
		map.put("rzsj", DateUtils.formatDate(hyxx.getRzsj()));
		map.put("szbm", hyxx.getSzbm());
		map.put("drzw", hyxx.getDrzw());
		map.put("skyh", hyxx.getSkyh());
		map.put("skyhkh", hyxx.getSkyhkh());
		map.put("skrxm", hyxx.getSkrxm());
		map.put("skyhdz", hyxx.getSkyhdz());
		map.put("lxr_jtxm", hyxx.getLxr_jtxm());
		map.put("lxr_jtgx", hyxx.getLxr_jtgx());
		map.put("lxr_jtdh", hyxx.getLxr_jtdh());
		map.put("lxr_jjxm", hyxx.getLxr_jjxm());
		map.put("lxr_jjgx", hyxx.getLxr_jjgx());
		map.put("lxr_jjdh", hyxx.getLxr_jjdh());
		map.put("jklx", jklx);
		return post(ServiceDefine.SVR_HYJKXX, map);
	}
	
	/**
	 * 获取我的财富信息
	 * @param hy_id
	 * @return
	 */
	public static JSONObject getMyWealth(int hy_id){
		return post(ServiceDefine.SVR_QUERYMYWEALTH, String.format("{hy_id=%s}", hy_id));
	}
	
	public static JSONObject getHyxx(int hy_id){
		return post(ServiceDefine.SVR_QUERYHYXX, String.format("{hy_id=%s}", hy_id));
	}
	
	public static JSONObject getJkcl(int hy_id, String jklx){
		return post(ServiceDefine.SVR_QUERYHYJKCL, String.format("{hy_id=%s, jklx=%s}", hy_id, jklx));
	}
	
	public static JSONObject getZlWsqk(int zh_id){
		return post(ServiceDefine.SVR_QUERYZLWSQK, String.format("{zh_id=%s}", zh_id));
	}
	
	public static JSONObject saveJkxx(int hy_id, String jklx, double jkje, int jkqx, String jkyt, int jdlv_id){
		Map<String, Object> map = Maps.newHashMap();
		map.put("hy_id", hy_id);
		map.put("jklx", jklx);
		map.put("jkje", jkje);
		map.put("jkqx", jkqx);
		map.put("jkyt", jkyt);
		map.put("jdlv_id", jdlv_id);
		return post(ServiceDefine.SVR_ADDJKXX, map);
	}
	
	public static JSONObject saveLcOrder(int hy_id, int lccp_id, double gmje, int fcbje){
		Map<String, Object> map = Maps.newHashMap();
		map.put("hy_id", hy_id);
		map.put("lccp_id", lccp_id);
		map.put("gmje", gmje);
		map.put("fcbje", fcbje);		
		return post(ServiceDefine.SVR_ORDERLCDD, map);
	}
	
	public static JSONObject queryHisJkqk(int hy_id){
		return post(ServiceDefine.SVR_QUERYHISJKQK, String.format("{hy_id=%s}", hy_id));
	}
	
	public static JSONObject queryCurJkqk(int hy_id){
		return post(ServiceDefine.SVR_QUERYCURJKQK, String.format("{hy_id=%s}", hy_id));
	}
	
	public static JSONObject queryLcxxByHyId(int hy_id){
		return post(ServiceDefine.SVR_QUERYLCXXBYHYID, String.format("{hy_id=%s}", hy_id));		
	}
	
	public static JSONObject queryFcbbgmx(int zh_id, int pagenum, int pagesize){
		return post(ServiceDefine.SVR_QUERYFCBMX, String.format("{zh_id=%s, pagenum=%s, pagesize=%s}", zh_id, pagenum, pagesize));		
	}
	
	public static JSONObject queryTsxx(int zh_id, int pagenum, int pagesize){
		return post(ServiceDefine.SVR_QUERYTSXX, String.format("{zh_id=%s, pagenum=%s, pagesize=%s}", zh_id, pagenum, pagesize));		
	}

	public static JSONObject queryPageZxxx(int pagenum, int pagesize){
		return post(ServiceDefine.SVR_QUERYPAGEZXXX, String.format("{pagenum=%s, pagesize=%s}", pagenum, pagesize));		
	}
	
	public static JSONObject queryZxxxById(int id){
		return post(ServiceDefine.SVR_QUERYZXXXBYID, String.format("{id=%s}", id));
	}
	
	public static JSONObject queryLatelyLcdd(int num){
		return post(ServiceDefine.SVR_QUERYLATELYLCDD, String.format("{num=%s}", num));
	}
	
	public static JSONObject lccpShow(){
		return post(ServiceDefine.SVR_LCCPSHOW, String.format("{}"));
	}
	
	public static JSONObject zxxxShow(){
		return post(ServiceDefine.SVR_ZXXXSHOW, String.format("{}"));
	}

	public static JSONObject verkey(int zh_id, String key){
		return post(ServiceDefine.SVR_VERKEY, String.format("{zh_id:%s, key:%s}", zh_id, key));
	}
	
	private static JSONObject post(String svr_, Map<String, Object> map) {
		String url = ServiceDefine.SERVICE_URL.concat(svr_);
		String params = JSONObject.fromObject(map).toString();
		String ret = HttpClientUtil.post(url, params);
		JSONObject jsonObj = JSONObject.fromObject(ret);
		return jsonObj;
	}

	private static JSONObject post(String svr_, String params) {
		String url = ServiceDefine.SERVICE_URL.concat(svr_);
		String ret = HttpClientUtil.post(url, params);
		JSONObject jsonObj = JSONObject.fromObject(ret);
		return jsonObj;
	}
}
